package com.ydd.controller.user;

import com.ydd.dto.LikeDTO;
import com.ydd.dto.QueryDTO;
import com.ydd.dto.YddArticleDTO;
import com.ydd.entity.YddUser;
import com.ydd.result.PageResult;
import com.ydd.result.Result;
import com.ydd.service.IYddArticleService;
import com.ydd.service.IYddUserDataService;
import com.ydd.service.IYddUserService;
import com.ydd.vo.PageResultVO;
import com.ydd.vo.UserArticleDataInfoVo;
import com.ydd.vo.YddUserInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@Api(tags = "用户相关")
@Slf4j
public class YddUserController {

    @Autowired
    private IYddUserService yddUserService;

    @Autowired
    private IYddArticleService yddArticleService;
    @Autowired
    private IYddUserDataService iYddUserDataService;
    /**
     * 获取用户信息
     *
     * @return
     */
    @GetMapping("/getUserInfo")
    @ApiOperation("获取用户信息")
    public Result<YddUserInfoVO> getUserInfo() {
        log.info("获取用户信息");
        return Result.success(yddUserService.getUserInfo());
    }

    /**
     * 获取我的文章
     *
     * @return
     */
    @GetMapping("/myArticle")
    @ApiOperation("获取我的文章")
    @Cacheable(value = "myArticle", key = "#queryDTO")
    public Result<PageResultVO> myArticle(QueryDTO queryDTO) {
        log.info("获取我的文章：分页：{}", queryDTO);
        return Result.success(yddArticleService.myArticle(queryDTO));
    }

    /**
     * 发布文章
     *
     * @param yddArticleDTO
     * @return
     */
    @PostMapping("/addArticle")
    @ApiOperation("发布文章")
    @CacheEvict(value = "myArticle", allEntries = true)
    public Result addArticle(@RequestBody YddArticleDTO yddArticleDTO) {
        log.info("发布文章：{}", yddArticleDTO);
        yddArticleService.addArticle(yddArticleDTO);
        return Result.success();
    }

    /**
     * 删除文章
     *
     * @param id
     * @return
     */
    @GetMapping("/delArticle")
    @ApiOperation("删除文章")
    @CacheEvict(value = "myArticle", allEntries = true)
    public Result delArticle(Integer id) {
        log.info("删除文章：{}", id);
        yddArticleService.delArticle(id);
        return Result.success();
    }

    /**
     * 编辑文章
     *
     * @param yddArticleDTO
     * @return
     */
    @PostMapping("/editArticle")
    @ApiOperation("编辑文章")
    @CacheEvict(value = "myArticle", allEntries = true)
    public Result editArticle(@RequestBody YddArticleDTO yddArticleDTO) {
        log.info("编辑文章：{}", yddArticleDTO);
        yddArticleService.editArticle(yddArticleDTO);
        return Result.success();
    }

    /**
     * 获取我的点赞/收藏
     *
     * @param type
     * @return
     */
    @GetMapping("/userDataList")
    @ApiOperation("获取我的点赞/收藏")
    @Cacheable(value = "userDataList", key = "#type")
    public Result<UserArticleDataInfoVo> userDataList(Integer type, QueryDTO queryDTO) {
        log.info("获取我的点赞/收藏：{},分页：{}", type, queryDTO);
        return Result.success(yddArticleService.userDataList(type, queryDTO));
    }

    /**
     * 更新用户信息
     *
     * @param yddUser
     * @return
     */
    @PostMapping("/update")
    @ApiOperation("更新用户信息")
    public Result update(@RequestBody YddUser yddUser) {
        log.info("更新用户信息：{}", yddUser);
        yddUserService.updateById(yddUser);
        return Result.success();
    }
    /**
     * 点赞取消点赞和收藏
     * @param dto
     * @return
     */
    @GetMapping("/userDataHandle")
    public Result userDataHandle(LikeDTO dto){
        iYddUserDataService.userDataHandle(dto);
        return Result.success();
    }
}
